/*
 * @Author: Zhang Yingying
 * @Date: 2020-12-14 21:51:16
 * @LastEditors: Zhang Yingying
 * @LastEditTime: 2020-12-14 21:51:18
 */
import { Message, MessageBox, Notification } from "element-ui";

const install = Vue => {
  /* 全局Message */
  Vue.prototype.$message = {};
  ["error", "success", "info"].forEach(type => {
    Vue.prototype.$message[type] = function(message) {
      Message({
        offset: 60,
        showClose: true,
        message,
        type: type,
        dangerouslyUseHTMLString: true,
        duration: 3000
      });
    };
  });

  Vue.prototype.$alert = ({ content, title, callback }) => {
    MessageBox.alert(content, title || "温馨提示", {
      confirmButtonText: "确定",
      dangerouslyUseHTMLString: true,
      callback: () => {
        if (callback) {
          callback();
        }
      }
    });
  };

  Vue.prototype.$confirm = ({ message, title, confirmed, canceled }) => {
    MessageBox.confirm(message, title || "温馨提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      closeOnClickModal: false,
      type: "warning"
    })
      .then(() => {
        if (confirmed) {
          confirmed();
        }
      })
      .catch(() => {
        if (canceled) {
          canceled();
        }
      });
  };

  Vue.prototype.$notify = (message, title, type, position) => {
    Notification({
      title: title,
      message: message,
      position: position || "top-right",
      type: type || "success",
      duration: 3000
    });
  };
};

export default install;
